package com.shd.edanpei.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jaeger.library.StatusBarUtil;
import com.shd.edanpei.R;
import com.shd.edanpei.ui.activity.SureLoanInfoActivity;
import com.shd.edanpei.ui.adapter.OrderAdapter;
import com.shd.edanpei.base.BaseFragment;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.base.rxbus.Subscribe;
import com.shd.edanpei.bean.StatusBean;
import com.shd.edanpei.presenter.contract.ManagerContract;
import com.shd.edanpei.enums.StatusEnum;
import com.shd.edanpei.presenter.ManagerPresenter;
import com.shd.edanpei.ui.activity.MeOrderDetailsActivity;
import com.shd.edanpei.ui.activity.SchemeActivity;
import com.shd.edanpei.ui.activity.ShareOrderDetailsActivity;
import com.shd.edanpei.ui.activity.SureOrderActivity;
import com.shd.edanpei.ui.view.ManageTopView;
import com.shd.edanpei.util.ToastUtil;
import com.shd.edanpei.view.RefreshLayout;

import butterknife.BindView;

/**
 * Created by mc on 2018/9/18.
 * 订单管理
 */

@SuppressWarnings("ALL")
public class ManagerFragment extends BaseFragment<ManagerPresenter> implements ManagerContract.ManagerIView, ManageTopView.Listener {

    @BindView(R.id.container)
    LinearLayout container;
    @BindView(R.id.refresh_layout)
    RefreshLayout refreshLayout;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    /**
     * 订单1级类型
     * 0:我的订单
     * 1:共享订单
     */
    private int mOrder1Type = 0;
    /**
     * 订单2级类型
     */
    private int mOrder2Type = 0;

    private Bundle bundle = new Bundle();
    private PopupWindow window;
    private OrderAdapter mAdapter;

    @Override
    protected Object setLayout() {
        return R.layout.fragment_manager;
    }

    @Override
    protected ManagerPresenter bindPresenter() {
        return new ManagerPresenter(this);
    }

    @Override
    protected void initView(@Nullable View view, @Nullable Bundle savedInstanceState) {
        RxBus.get().register(this);
        container.addView(new ManageTopView(getContext(), this), 0);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new OrderAdapter(getContext(), mOrder1Type);
        recyclerView.setAdapter(mAdapter);
        refreshLayout.setRefreshing(true);

        refresh(mOrder1Type, mOrder2Type);
    }


    @Override
    protected void initListener() {
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh(mOrder1Type, mOrder2Type);
            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                bundle.clear();
                bundle.putInt(Constant.ORDER_ID, mAdapter.getItem(position).getOrderId());
                bundle.putInt(Constant.ORDER_TYPE, mAdapter.getItem(position).getOrderType());
                bundle.putInt(Constant.INDEX, position);
                switch (mOrder1Type) {
                    case Constant.ME_ORDER:
                        //发单订单详情
                        startActivity(MeOrderDetailsActivity.class, bundle);
                        break;
                    case Constant.SHARE_ORDER:
                        //接单订单详情
                        startActivity(ShareOrderDetailsActivity.class, bundle);
                        break;
                    default:
                        break;
                }

            }
        });
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                final int orderID = mAdapter.getItem(position).getOrderId();
                final int orderType = mAdapter.getItem(position).getOrderType();
                bundle.clear();
                bundle.putInt(Constant.ORDER_ID, orderID);
                bundle.putInt(Constant.ORDER_TYPE, orderType);
                bundle.putInt(Constant.INDEX, position);
                switch (view.getId()) {
                    case R.id.tv_cancel:
                        //撤销
                        if (mOrder1Type == Constant.ME_ORDER) {
                            mPresenter.userUndo(orderID, orderType, position, mAdapter.getItem(position).isIsPayDeposit());
                        } else if (mOrder1Type == Constant.SHARE_ORDER) {
                            mPresenter.bankUndo(orderID, orderType, position, mAdapter.getItem(position).isIsPayDeposit());
                        }
                        break;
                    case R.id.tv_goCreate:
                        //继续创建
                        mPresenter.againApply(orderID, orderType, position);
                        break;
                    case R.id.tv_first:
                        //初审
                        bundle.putInt(Constant.TAG, 0);
                        startActivity(SchemeActivity.class, bundle);
                        break;
                    case R.id.tv_end:
                        //终审
                        bundle.putInt(Constant.TAG, 1);
                        startActivity(SchemeActivity.class, bundle);
                        break;
                    case R.id.tv_sure:
                        if (mOrder1Type == Constant.ME_ORDER) {
                            //确认收款
                            mPresenter.sureGetLone(bundle);
                        } else if (mOrder1Type == Constant.SHARE_ORDER) {
                            //确认放款
                            startActivity(SureOrderActivity.class, bundle);
                        }

                        break;
                    case R.id.tv_remove:
                        //移除
                        if (mOrder1Type == Constant.ME_ORDER) {
                            mPresenter.userRemove(orderID, orderType, position);
                        } else if (mOrder1Type == Constant.SHARE_ORDER) {
                            mPresenter.bankRemove(orderID, orderType, position);
                        }
                        break;
                    case R.id.tv_updata:
                        //修改订单
                        bundle.putString(Constant.ADDRESS, mAdapter.getItem(position).getArea());
                        bundle.putInt(Constant.CUSTOMER_ID, mAdapter.getItem(position).getCustomerId());
                        mPresenter.updataOrder(bundle, orderID, orderType, mAdapter.getItem(position).getUserStatus());
                        break;
                    case R.id.tv_service:
                        Bundle bundle = new Bundle();
                        bundle.putInt(Constant.ORDER_ID, orderID);
                        startActivity(SureLoanInfoActivity.class, bundle);
                        break;
//                    case R.id.tv_sureUpdate:
//                        //确认修改
//                        if (mOrder1Type == Constant.ME_ORDER) {
//                            mPresenter.sureUpdate(orderID, mAdapter.getItem(position).getUserStatus(), position);
//                        }
//                        break;
                }
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.loadMore(mOrder1Type, mOrder2Type);
            }
        }, recyclerView);
    }

    @Override
    public OrderAdapter getAdapter() {
        return mAdapter;
    }

    @Override
    public RefreshLayout getRefreshLayout() {
        return refreshLayout;
    }

    @Override
    public void setUnRead(boolean newMessageNotRead) {

    }

    @Override
    public void onResume() {
        super.onResume();
        StatusBarUtil.setLightMode(getActivity());

    }

    //刷新列表
    @Subscribe(code = RxBusCode.REFRESH_ORDER_STATUS)
    public void refreshStatus(StatusBean bean) {
        if (mOrder1Type == Constant.ME_ORDER) {
            mAdapter.getData().get(bean.getIndex()).setUserStatus(bean.getOrderStatus());
        } else if (mOrder1Type == Constant.SHARE_ORDER) {
            mAdapter.getData().get(bean.getIndex()).setManagerStatus(bean.getOrderStatus());
        }
        if (bean.getOrderStatus() == StatusEnum.deleted.getStatus()) {
            mAdapter.remove(bean.getIndex());
        }
        mAdapter.notifyItemChanged(bean.getIndex());
    }

    @Subscribe(code = RxBusCode.UPDATE_LOCATION_SUCCESS)
    public void updateLocation(StatusBean bean) {
        mAdapter.getItem(bean.getIndex()).setArea(bean.getMsg());
        mAdapter.notifyItemChanged(bean.getIndex());
    }

    @Subscribe(code = RxBusCode.UPDATE_CUSTOMER_INFO_SUCCESS)
    public void updateName(StatusBean bean) {
        mAdapter.getItem(bean.getIndex()).setCustomerName(bean.getMsg());
        mAdapter.notifyItemChanged(bean.getIndex());
    }

    @Override
    public void onDestroyView() {
        container.removeAllViews();
        RxBus.get().unRegister(this);
        super.onDestroyView();
    }


    /**
     * 刷新列表
     *
     * @param order1Type 1级类型
     * @param order2Type 2级类型
     */
    @Override
    public void refresh(int order1Type, int order2Type) {
        refreshLayout.setRefreshing(true);
        this.mOrder1Type = order1Type;
        this.mOrder2Type = order2Type;
        mAdapter.setOrder1Type(mOrder1Type);
        mPresenter.refresh(mOrder1Type, mOrder2Type);//第一次进来  1  0， 无数据   有数据  0  ， 0
    }


}
